package view;

import java.io.InputStream;
import java.util.Locale;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;

import view.resources.Images;

import control.Storage;
import control.Util;

public class LanguageSelector {

	private Button english, hungarian;
	private Display display;
	private Shell shell;
	
	public LanguageSelector(){
		
		display = Display.getCurrent() != null ? Display.getCurrent() : new Display();
		shell = new Shell(display);
		configureShell();
		keepAlive();
	}
	
	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(2, false);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}
	
	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText("LANGUAGE");
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
		shell.setFocus();
	}
	
	
	private void buildContent() {
		
		setIcons();
		english = new Button(shell, SWT.PUSH);
		english.setImage(Images.ENGLISH.createImage());
		english.addSelectionListener(new SelectionListener() {

	        @Override
	        public void widgetSelected(SelectionEvent e) {
	        	buttonPressed(true);
	        }

	        @Override
	        public void widgetDefaultSelected(SelectionEvent e) {
	            // TODO Auto-generated method stub
	        		
	        }
	    });
		
		hungarian = new Button(shell, SWT.PUSH);
		hungarian.setImage(Images.HUNGARIAN.createImage());
		hungarian.addSelectionListener(new SelectionListener() {

	        @Override
	        public void widgetSelected(SelectionEvent e) {
	        	buttonPressed(false);
	        }

	        @Override
	        public void widgetDefaultSelected(SelectionEvent e) {
	            // TODO Auto-generated method stub
	        		
	        }
	    });

	}
	
	private void setIcons(){
		
		InputStream in3 = this.getClass().getResourceAsStream("/res/card_16.png");
		final Image small = new Image(display, in3);
		InputStream in4 = this.getClass().getResourceAsStream("/res/card_32.png");
		final Image large = new Image(display, in4);
		final Image[] images = new Image[] { small, large };
		shell.setImages(images);
	}
	
	
	private void buttonPressed(Boolean flag){
		 Locale currentLocale;
		 if(flag)
		 {
			 currentLocale = new Locale("en","EN" );
		 }
		 else
		 {
			 currentLocale = new Locale("hu", "HU");
		     
		 }
		 
	     Storage.getInstance().setMessages(ResourceBundle.getBundle("res/MessagesBundle", currentLocale));
	     shell.dispose();
	}
	
	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}
}
